package com.wwwarehouse.usercenter.fragment.permissiondistribute;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.adapter.AuthorityDistributeAllDetailRvAdapter;
import com.wwwarehouse.usercenter.bean.authoritydistribute.GetSuccessDetailResponseBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author : mundane
 * @time : 2017/9/24 13:45
 * @description :
 * @file : AuthorityDistributeSuccessDetailListFragment.java
 */

public class AuthorityDistributeSuccessDetailListFragment extends BaseFragment {
	private RecyclerView mRecyclerView;
	private AuthorityDistributeAllDetailRvAdapter mRvAdapter;
	private static final String KEY_PAGE = "key_page";
	private static final String KEY_PROCESS_ID = "key_process_id";
	private String page;
	private String mProcessId;
	private List<GetSuccessDetailResponseBean.ListBean> mDataList;
	private NoHttpUtils.OnResponseListener mResponseListener = new NoHttpUtils.OnResponseListener() {
		@Override
		public void onSucceed(CommonClass commonClass, int what) {
			switch (what) {
				case 0:
					if (TextUtils.equals("0", commonClass.getCode())) {
						String data = commonClass.getData().toString();
//						LogUtils.showInfoLog("data = " + data);
						GetSuccessDetailResponseBean bean = JSON.parseObject(data, GetSuccessDetailResponseBean.class);
						mDataList = bean.list;
						mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
						mRvAdapter = new AuthorityDistributeAllDetailRvAdapter(mDataList);
						mRecyclerView.setAdapter(mRvAdapter);
					} else {
						ToastUtils.showToast(commonClass.getMsg());
					}
					break;
			}
		}

		@Override
		public void onFailed(String s, int i) {

		}

		@Override
		public void onFinish(int i) {

		}
	};

	public static AuthorityDistributeSuccessDetailListFragment newInstance(String page, String processId) {
		AuthorityDistributeSuccessDetailListFragment fragment = new AuthorityDistributeSuccessDetailListFragment();
		Bundle bundle = new Bundle();
		bundle.putString(KEY_PAGE, page);
		bundle.putString(KEY_PROCESS_ID, processId);
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle = getArguments();
		if (bundle != null) {
			page = bundle.getString(KEY_PAGE);
			mProcessId = bundle.getString(KEY_PROCESS_ID);
		}
		mDataList = new ArrayList<>();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_list_detail_distribute_authority, container, false);
		return view;
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		// findViewById
		mRecyclerView = $(R.id.rv);
		if (!TextUtils.isEmpty(page)) {
			Map<String, Object> map = new HashMap<>();
			map.put("page", page);
			map.put("processId", mProcessId);
			map.put("size", AuthorityDistributeAllAuthDetailPageFragment.PAGE_SIZE);
			NoHttpUtils.httpPost(UserCenterConstant.GET_SUCCESS_DETAIL, map, mResponseListener, 0);
		}
	}

	@Override
	public void setTitle() {

	}

	@Override
	public void requestDatas() {

	}
}
